package C10_12_ReplaceConstructorWithFactoryMethod.example2.after_refactor;

import C10_12_ReplaceConstructorWithFactoryMethod.example2.before_refactor.Engineer;
import C10_12_ReplaceConstructorWithFactoryMethod.example2.before_refactor.Manager;
import C10_12_ReplaceConstructorWithFactoryMethod.example2.before_refactor.Salesman;

/**
 * 根据字符串创建子类对象
 * 缺点：失去了编译期检验，向用户暴露了子类名称
 */
public class Employee {
    static final int ENGINEER = 0;
    static final int SALESMAN = 1;
    static final int MANAGER = 2;

    static Employee create(String name) {
        try {
            return (Employee) Class.forName(name).newInstance();
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to instantiate " + name);
        }
    }
}
